package org.example.managementsystem.controller;


import org.example.managementsystem.model.Product_Plan_Detail;
import org.example.managementsystem.service.Product_Plan_DetailService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/Product_Plan_Details")
@CrossOrigin(origins = "*")
public class Product_Plan_DetailController {
    private final Product_Plan_DetailService productPlanDetailService;

    public Product_Plan_DetailController(Product_Plan_DetailService productPlanDetailService) {
        this.productPlanDetailService = productPlanDetailService;
    }


    /**
     * 获取所有人员信息
     */
    @GetMapping
    public ResponseEntity<List<Product_Plan_Detail>> getAllProduct_Plan_Details() {
        List<Product_Plan_Detail> product_plan_details = productPlanDetailService.getAllProduct_Plan_Details();
        return ResponseEntity.ok(product_plan_details);
    }

    /**
     * 根据ID获取人员信息
     */
    @GetMapping("/{detail_id}")
    public ResponseEntity<Product_Plan_Detail> getProduct_Plan_DetailById(@PathVariable Long detail_id) {
        Product_Plan_Detail product_plan_detail= productPlanDetailService.getProduct_Plan_DetailById(detail_id);
        if (product_plan_detail != null) {
            return ResponseEntity.ok(product_plan_detail);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 添加人员
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addProduct_Plan_Detail(@RequestBody Product_Plan_Detail product_plan_detail) {
        Product_Plan_Detail savedProduct_Plan_Detail = productPlanDetailService.addProduct_Plan_Detail(product_plan_detail);
        Map<String, Object> response = new HashMap<>();
        response.put("detail_id", savedProduct_Plan_Detail.getDetail_id());
        response.put("message", "人员添加成功");
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新人员信息
     */
    @PutMapping("/{detail_id}")
    public ResponseEntity<Map<String, String>> updateProduct_Plan_Detail(@PathVariable Long detail_id, @RequestBody Product_Plan_Detail product_plan_detail) {
        product_plan_detail.setDetail_id(detail_id);
        boolean updated = productPlanDetailService.updateProduct_Plan_Detail(product_plan_detail);

        Map<String, String> response = new HashMap<>();
        if (updated) {
            response.put("message", "明细更新成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "明细更新失败，未找到指定ID的明细");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 删除人员
     */
    @DeleteMapping("/{detail_id}")
    public ResponseEntity<Map<String, String>> deleteProduct_Plan_Detail(@PathVariable Long detail_id) {
        boolean deleted = productPlanDetailService.deleteProduct_Plan_Detail(detail_id);

        Map<String, String> response = new HashMap<>();
        if (deleted) {
            response.put("message", "明细删除成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "明细删除失败，未找到指定ID的明细");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }
    // Product_Plan_DetailController.java
    @GetMapping("/plan/{plan_id}")
    public ResponseEntity<List<Product_Plan_Detail>> getDetailsByPlanId(@PathVariable Long plan_id) {
        List<Product_Plan_Detail> details = productPlanDetailService.getDetailsByPlanId(plan_id);
        return ResponseEntity.ok(details);
    }
//    // Product_Plan_DetailController.java
//    @PutMapping("/{detail_id}")
//    public ResponseEntity<Map<String, String>> updateProduct_Plan_Detail(
//            @PathVariable Long detail_id, @RequestBody Product_Plan_Detail detail) {
//        detail.setDetail_id(detail_id);
//        boolean updated = productPlanDetailService.updateProduct_Plan_DetailById(detail);
//        return updated ?
//                ResponseEntity.ok(Map.of("message", "明细更新成功")) :
//                ResponseEntity.notFound().build();
//    }
}
